package com.villaday.idf.fragment3;

import android.content.Intent;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.activity.NewAndEditAddrActivity;
import com.villaday.idf.adapter3.AddressAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.custom.LoadDialog;
import com.villaday.idf.model2.ShippingAddressesEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CommonAddressFragment extends BaseFragment implements View.OnClickListener, AbsRecyclerViewAdapter.OnItemClickListener {


    @BindView(R.id.rv_ca)
    RecyclerView rvCa;
    @BindView(R.id.tv_ca)
    TextView tvCa;
    @BindView(R.id.tv_emptyTips)
    TextView tvEmptyTips;
    @BindView(R.id.tv_perchase)
    TextView tvPerchase;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    @BindView(R.id.srl_ca)
    SwipeRefreshLayout srlCa;
    @BindView(R.id.ll_cg)
    LinearLayout llCg;


    private LoadDialog loadDialog;
    private Gson gson = new GsonBuilder().create();

    private ShippingAddressesEntity shippingAddressesEntity;
    private AddressAdapter mAddressAdapter;


    @Override
    protected int getViewResId() {
        return R.layout.fragment_commom_address;
    }


    public static CommonAddressFragment instance() {
        CommonAddressFragment commonAddressFragment = new CommonAddressFragment();
        return commonAddressFragment;
    }


    @Override
    protected void init(View v) {
        super.init(v);
        srlCa.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getAddress();
            }
        });
        initAddressRecycle();
        showDialog();
        getAddress();
    }


    private void initAddressRecycle() {
        rvCa.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mAddressAdapter = new AddressAdapter(getActivity(), this);
        rvCa.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(getActivity(), 10)
                , getResources().getColor(R.color.color2BG), PhoneUtil.dp2px(getContext(), 10)));
        rvCa.setAdapter(mAddressAdapter);
        mAddressAdapter.setOnItemClickListener(this);
        mAddressAdapter.setEmptyView(R.layout.empty_tips);
        mAddressAdapter.setEmptyTips(R.id.tv_emptyTips, "");
    }

    private void getAddress() {

        Map<String, String> map = new HashMap<>();
        OkHttpUtil.getJSON(getActivity(), Constant.URL.getAddresses, map, new OkHttpUtil.OnDataListener() {

            @Override
            public void onResponse(String url, String json) {
                try {
                    LogUtil.jLog().e(json);
                    if (!TextUtils.isEmpty(json)) {
                        dismissLoading();
                        shippingAddressesEntity = gson.fromJson(json, ShippingAddressesEntity.class);
                        if ("200".equals(shippingAddressesEntity.getCode()) && shippingAddressesEntity.getBody().size() > 0) {
                            rvCa.setVisibility(View.VISIBLE);
                            mAddressAdapter.setData(shippingAddressesEntity.getBody());
                        } else {
                            rvCa.setVisibility(View.GONE);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), NewAndEditAddrActivity.class);
        intent.putExtra("type", 1);
        intent.putExtra("entity", shippingAddressesEntity.getBody().get((Integer) v.getTag()));
        startActivityForResult(intent, 123);
    }

    private void dismissLoading() {
        if (loadDialog != null) {
            loadDialog.dismiss();//获取信息后关闭对话框
        }

        if (srlCa != null && srlCa.isRefreshing()) {
            srlCa.setRefreshing(false);
        }
    }

    protected void showDialog() {
        if (loadDialog == null) {
            loadDialog = LoadDialog.newInstance("加载中");
        }
        loadDialog.show(getActivity().getSupportFragmentManager(), "loading");
    }


    @Override
    public void onItemClick(View v, int position) {

    }


    @OnClick({R.id.tv_ca})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.tv_ca:
//                startActivityResult(AddressAddActivity.class, Constant.Code.addressAddCode);
                Intent intent = new Intent(getActivity(), NewAndEditAddrActivity.class);
                if (shippingAddressesEntity != null && shippingAddressesEntity.getBody() != null && shippingAddressesEntity.getBody().size() == 0) {
                    intent.putExtra("isSelect", true);
                }
                startActivityForResult(intent, 123);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            showDialog();
            getAddress();
        }
    }
}
